package com.example.administrator.systemfailreport.activity;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.TextView;

import com.example.administrator.systemfailreport.R;
import com.example.administrator.systemfailreport.adapter.SysErrorFragAdapter;
import com.example.administrator.systemfailreport.utils.SwipeRefreshUtil;

import java.util.List;

import entity.SysErrorEntity;

/**
 * A simple {@link Fragment} subclass.
 */
public class SysErrorFragment extends Fragment implements OnRefreshListener{


    private Context context;
    private String userId;
    private ListView lv;
    private TextView tv_Empity;
    private SwipeRefreshLayout swipeView;
    List<SysErrorEntity> data;
    private SysErrorFragAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=getContext();
        userId=getArguments().getString("userId");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View contentView=inflater.inflate(R.layout.fragment_sys_error,container,false);
        initView(contentView);
        initData();
        return contentView;
    }

    private void initData() {
        if (data==null||"".equals(data)){
            tv_Empity.setVisibility(View.GONE);
        }else{
            adapter=new SysErrorFragAdapter(context,data);
            lv.setAdapter(adapter);

        }
        SwipeRefreshUtil.onScroolSetSwipeRefreshEvent(lv, swipeView);
    }

    private void initView(View contentView) {
        lv = (ListView) contentView.findViewById(R.id.lv_uploadvideo);
        tv_Empity = (TextView) contentView.findViewById(R.id.tv_listempity);
        swipeView = (SwipeRefreshLayout) contentView.findViewById(R.id.sref_videoloading);
        swipeView.setOnRefreshListener(this);
        swipeView.setDistanceToTriggerSync(50);
    }


    @Override
    public void onRefresh() {


        if (adapter != null)
            adapter.notifyDataSetChanged();

        swipeView.setRefreshing(false);
     }
}
